package com.kindnesslibrary.pullrefresh;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;

import com.kindnesslibrary.util.WidgetUtil;

/**
 * Created by lenovo on 2017/7/5.
 */
public class PtrRecyclerView extends RecyclerView {

    private View childView;
    private int scrollY;
    private Rect normal = new Rect();

    public PtrRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        childView = this;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                scrollY = y;
            }
            break;
            case MotionEvent.ACTION_MOVE: {
                final float preY = scrollY;
                float nowY = event.getY();
                int deltaY = (int) (preY - nowY) / 4;
                int yy = childView.getTop() - deltaY;
                scrollY = y;
                if(isNeedMove()){
                    if (normal.isEmpty()) {
                        // 保存正常的布局位置
                        normal.set(childView.getLeft(), childView.getTop(),
                                childView.getRight(), childView.getBottom());
                        break;
                    }
                    childView.layout(childView.getLeft(), yy,
                            childView.getRight(), (childView.getBottom() - deltaY));
                }
            }
            break;
            case MotionEvent.ACTION_UP:{
                if(!normal.isEmpty()){
                    animateUp();
                }
            }
            break;
        }
        return super.onTouchEvent(event);
    }

    public boolean isNeedMove() {
        if (WidgetUtil.getScollYDistance(this) == 0 || isCanPullUp()) {
            return true;
        }
        return false;
    }

    public void animateUp() {
        // 开启移动动画
        TranslateAnimation ta = new TranslateAnimation(0, 0, childView.getTop(),
                normal.top);
        ta.setDuration(200);
        childView.startAnimation(ta);
        // 设置回到正常的布局位置
        childView.layout(normal.left, normal.top, normal.right, normal.bottom);
        normal.setEmpty();
    }

    private boolean isCanPullUp() {
        final RecyclerView.Adapter adapter = ((RecyclerView) childView).getAdapter();
        if (null == adapter) {
            return true;
        }

        final int lastItemPosition = adapter.getItemCount() - 1;
        final int lastVisiblePosition = ((LinearLayoutManager) ((RecyclerView) childView).getLayoutManager()).findLastVisibleItemPosition();

        if (lastVisiblePosition >= lastItemPosition) {
            final int childIndex = lastVisiblePosition - ((LinearLayoutManager) ((RecyclerView) childView).getLayoutManager()).findFirstVisibleItemPosition();
            final int childCount = ((RecyclerView) childView).getChildCount();
            final int index = Math.min(childIndex, childCount - 1);
            final View lastVisibleChild = ((RecyclerView) childView).getChildAt(index);
            if (lastVisibleChild != null) {
                return lastVisibleChild.getBottom() <= childView.getBottom() - childView.getTop();
            }
        }

        return false;
    }

}
